package PTA_JAVA.JavaA.Javademo18;

import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //4.1 输入整型值n，然后建立n个不同的形状。如果输入rect，则依次输入宽、长。如果输入cir，则输入半径。
        int n = sc.nextInt();
        Shape[] a = new Shape[n];
        for (int i = 0; i < n; i++) {
            String s = sc.next();
            if (s.equals("rect")) {
                a[i] = new Rectangle(sc.nextInt(), sc.nextInt());
            } else {
                a[i] = new Circle(sc.nextInt());
            }

        }
        System.out.println(sumAllPerimeter(a));
        System.out.println(sumAllArea(a));
        //4.2 然后输出所有的形状的周长之和，面积之和。并将所有的形状信息以样例的格式输出。 提示：使用Arrays.toString。

        System.out.println(Arrays.toString(a));

        for (int i = 0; i < n; i++) {
            System.out.println(a[i].getClass() + "," + a[i].getClass().getSuperclass());
        }
        //4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass() //获得类型, shape.getClass().getSuperclass() //获得父类型;
    }

    //3.编写double sumAllArea方法计算并返回传入的形状数组中所有对象的面积和与
    //double sumAllPerimeter方法计算并返回传入的形状数组中所有对象的周长和。
    public static double sumAllArea(Shape[] a) {
        double sum = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] instanceof Circle) {
                sum += ((Circle) a[i]).getArea();
            } else {
                sum += ((Rectangle) a[i]).getArea();
            }
        }
        return sum;
    }

    public static double sumAllPerimeter(Shape[] a) {
        double sum = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] instanceof Circle) {
                sum += ((Circle) a[i]).getPerimeter();
            } else {
                sum += ((Rectangle) a[i]).getPerimeter();
            }
        }
        return sum;
    }
}

abstract class Shape {
    static final double PI = 3.14;

    public abstract double getPerimeter();

    public abstract double getArea();
}

class Rectangle extends Shape {
    private int width, length;

    public Rectangle(int width, int length) {
        super();
        this.width = width;
        this.length = length;
    }

    @Override
    public double getPerimeter() {
        return (width + length) * 2;
    }

    @Override
    public double getArea() {
        return width * length;
    }

    @Override
    public String toString() {
        return "Rectangle [" +
                "width=" + width +
                ", length=" + length +
                ']';
    }
}

class Circle extends Shape {
    private int radius;

    public Circle(int radius) {
        super();
        this.radius = radius;
    }

    @Override
    public double getPerimeter() {
        return 2 * PI * radius;
    }

    @Override
    public double getArea() {
        return PI * radius * radius;
    }

    @Override
    public String toString() {
        return "Circle [" +
                "radius=" + radius +
                ']';
    }
}
/*
计算圆形的面积与周长，使用Math.PI。
求周长和面积时，应先计算出其值(带小数位)，然后强制转换为int再返回。
 */